<template>
  <div class="echarts" id="main"></div>
</template>

<script>
import * as echarts from "echarts";
import chinaMap from "@/assets/json/china.json";
// import $ from "jquery";
export default {
  name: "a-map",
  data() {
    return {
      geoCoordMap1: {
        黑龙江: [127.9688, 45.368],
        内蒙古: [110.3467, 41.4899],
        吉林: [125.8154, 44.2584],
        北京: [116.4551, 40.2539],
        辽宁: [123.1238, 42.1216],
        河北: [114.4995, 38.1006],
        天津: [117.4219, 39.4189],
        山西: [112.3352, 37.9413],
        陕西: [109.1162, 34.2004],
        甘肃: [103.5901, 36.3043],
        宁夏: [106.3586, 38.1775],
        青海: [101.4038, 36.8207],
        新疆: [87.9236, 43.5883],
        西藏: [91.11, 29.97],
        四川: [103.9526, 30.7617],
        重庆: [108.384366, 30.439702],
        山东: [117.1582, 36.8701],
        河南: [113.4668, 34.6234],
        江苏: [118.8062, 31.9208],
        安徽: [117.29, 32.0581],
        湖北: [114.3896, 30.6628],
        浙江: [119.5313, 29.8773],
        福建: [119.4543, 25.9222],
        江西: [116.0046, 28.6633],
        湖南: [113.0823, 28.2568],
        贵州: [106.6992, 26.7682],
        云南: [102.9199, 25.4663],
        广东: [113.12244, 23.009505],
        广西: [108.479, 23.1152],
        海南: [110.3893, 19.8516],
        上海: [121.4648, 31.2891],
      },
      geoCoordMap: [
        { value: [127.9688, 45.368], name: "黑龙江", number: "120" },
        { value: [110.3467, 41.4899], name: "内蒙古", number: "10" },
        { value: [125.8154, 44.2584], name: "吉林", number: "20" },
        { value: [116.4551, 40.2539], name: "北京", number: "14.28" },
        { value: [114.4995, 38.1006], name: "辽宁", number: "0.45" },
        { value: [117.4219, 39.4189], name: "天津", number: "0.43" },
        { value: [114.4995, 38.1006], name: "河北", number: "0.8" },
        { value: [112.3352, 37.9413], name: "山西", number: "0.03" },
        { value: [109.1162, 34.2004], name: "陕西", number: "1.74" },
        { value: [103.5901, 36.3043], name: "甘肃", number: "0" },
        { value: [106.3586, 38.1775], name: "宁夏", number: "0.21" },
        { value: [101.4038, 36.8207], name: "青海", number: "0.01" },
        { value: [87.9236, 43.5883], name: "新疆", number: "0.16" },
        { value: [91.11, 29.97], name: "西藏", number: "0" },
        { value: [103.9526, 30.7617], name: "四川", number: "9.27" },
        { value: [108.384366, 30.439702], name: "重庆", number: "6.66" },
        { value: [117.1582, 36.8701], name: "山东 +10", number: "59.41" },
        { value: [113.4668, 34.6234], name: "河南", number: "0.80" },
        { value: [118.8062, 31.9208], name: "江苏", number: "5.55" },
        { value: [117.29, 32.0581], name: "安徽", number: "0" },
        { value: [114.3896, 30.6628], name: "湖北", number: "3.06" },
        { value: [119.5313, 29.8773], name: "浙江", number: "14.62" },
        { value: [119.4543, 25.9222], name: "福建", number: "5.73" },
        { value: [116.0046, 28.6633], name: "江西", number: "0.05" },
        { value: [113.0823, 28.2568], name: "湖南", number: "0.38" },
        { value: [106.6992, 26.7682], name: "贵州", number: "1.74" },
        { value: [102.9199, 25.4663], name: "云南", number: "0.16" },
        { value: [113.12244, 23.009505], name: "广东", number: "20.95" },
        { value: [110.3893, 19.8516], name: "广西", number: "1.03" },
        { value: [106.6992, 26.7682], name: "海南", number: "0.45" },
        { value: [121.4648, 31.2891], name: "上海", number: "10.17" },
      ],
    };
  },
  created() {},
  mounted() {
    var chartDom = document.getElementById("main");
    echarts.registerMap("china", chinaMap);
    var myChart = echarts.init(chartDom);

    var option;
    option = {
      backgroundColor: "#090F27",
      tooltip: {
        triggerOn: "onmousemove",
      },
      legend: {
        show: false,
      },
      geo: [
        {
          type: "map",
          map: "china",
          itemStyle: {
            areaColor: "rgba(255,0,100,1)",
            borderWidth: 0.8, //设置外层边框
            borderColor: "#223055",
            shadowColor: "rgba(22, 118, 179)",
            shadowBlur: 2,
            shadowOffsetX: -4,
            shadowOffsetY: 6,
          },
          emphasis: {
            disabled: true,
          },
          z: 98,
          // itemStyle: {
          //   shadowColor: "rgba(22, 118, 179)",
          //   shadowBlur: 2,
          //   shadowOffsetX: -4,
          //   shadowOffsetY: 6,
          // },
          // 设置单独的样式
          regions: [
            {
              name: "南海诸岛",
              itemStyle: {
                opacity: 0,
                label: {
                  show: false,
                },
              },
            },
          ],
          tooltip: { show: false },
        },
      ],
      series: [
        {
          type: "map",
          map: "china",
          z: 100,
          itemStyle: {
            areaColor: "rgba(255,110,120,1)",
            borderWidth: 0.8, //设置外层边框
            borderColor: "#223055",
          },
          // 鼠标悬浮的操作；也可以设置鼠标悬浮的样式同select一样
          emphasis: {
            label: {
              show: true,
            },
            itemStyle: {
              areaColor: "rgba(255,110,120,.5)",
            },
          },
          // 鼠标点击选中的样式
          select: {
            disabled: true,
          },
          tooltip: { show: false },
          data: [
            // {
            //   name: "西藏",
            //   value: 100,
            //   label: { show: true }, //有数据的省份显示label
            // },
            // {
            //   name: "台湾",
            //   value: 100,
            //   label: { show: true }, //有数据的省份显示label
            // },
            // {
            //   name: "北京",
            //   value: 100,
            //   label: { show: true }, //有数据的省份显示label
            // },
          ],
          label: {
            formatter: (v) => {
              return `{b1|${v.name}}\n{b2| }`;
            },
            distance: -(18 + 20),
            rich: {
              b1: {
                width: 72,
                height: 18,
                backgroundColor: "gold",
                // backgroundColor: "#999999",
                align: "center",
                color: "#000",
                fontWeight: 700,
                fontSize: 14,
                padding: [2, 0, 0, 0],
              },
              b2: {
                width: 12,
                height: 12,
                align: "center",
                backgroundColor: "#999999",
              },
            },
          },
        },
      ],
    };
    myChart.setOption(option);
    myChart.getZr().on("click", function (params) {
      var pixelPoint = [params.offsetX, params.offsetY];
      var dataPoint = myChart.convertFromPixel({ geoIndex: 0 }, pixelPoint);
      console.log(dataPoint);
    });
    // let bu = 0;
    requestAnimationFrame(() => {
      const arr = [];
      this.geoCoordMap.forEach((item) => {
        let count = Math.ceil(item.number);
        let time = count > 0 ? Math.ceil((60 * 1000) / count) : 0;
        time = time < 1000 ? 1000 : time;
        console.log(count, "count");
        console.log(time, "time");
        if (count > 0) {
          let timeId = setInterval(() => {
            myChart.dispatchAction({
              type: "highlight",
              seriesIndex: 0, //这行不能省
              // dataIndex: inx,
              name: item.name,
            });
            // let time2 = time > 1000 ? 1000 : time - 100;
            let timeOutId = setTimeout(() => {
              myChart.dispatchAction({
                type: "downplay",
                seriesIndex: 0, //这行不能省
                // dataIndex: inx,
                name: item.name,
              });
              clearTimeout(timeOutId);
            }, 500);
          }, time);
          arr.push(timeId);
        }
      });
    });
  },
  methods: {},
};
</script>
<style scoped>
.echarts {
  width: 1000px;
  height: 1000px;
}
</style>
